package com.boomsecret.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wangjinliang on 2019/1/31.
 */
public class DeadLockDemo {
    private static final ReentrantLock LOCK_A = new ReentrantLock();
    private static final ReentrantLock LOCK_B = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(() -> {
            LOCK_B.lock();
            System.out.println("child thread obtain lock b");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("child thread want to obtain lock a");
            LOCK_A.lock();
        }).start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LOCK_A.lock();
        System.out.println("main thread obtain lock a");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main thread want to obtain lock b");
        LOCK_B.lock();
    }
}
